-
Notifications
You must be signed in to change notification settings - Fork 9
/
ChordProviderApp.swift
192 lines (177 loc) · 6.59 KB
/
ChordProviderApp.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
//
// ChordProviderApp.swift
// Chord Provider
//
// © 2024 Nick Berendsen
//
import SwiftUI
import ChordProShared
import SwiftlyChordUtilities
/// SwiftUI `Scene` for **Chord Provider**
///
/// This is the starting point of the application
///
/// - Note: Each platform has its own `body`
@main struct ChordProviderApp: App {
/// The observable ``FileBrowser`` class
@State private var fileBrowser = FileBrowser()
/// The state of the app
@State private var appState = AppState(id: "Main")
/// The ``AppDelegate`` class for **Chord Provider**
@NSApplicationDelegateAdaptor private var appDelegate: AppDelegate
/// The `openWindow` environment to open a new Window
@Environment(\.openWindow) private var openWindow
/// The body of the `Scene`
///
/// - `Window`: scene with a list of songs
/// - `Window`: scene to export a folder of songs
/// - `DocumentGroup` scene to open a single song
/// - `MenuBarExtra` scene that shows a list with songs as well
/// - `Settings` scene to open the settings Window
var body: some Scene {
// MARK: 'Song List' single window
/// The 'Song List' window
Window("Song List", id: AppSceneID.songListWindow.rawValue) {
FileBrowserView()
.environment(fileBrowser)
}
.keyboardShortcut("l")
/// Make it sizeable by the View frame
.windowResizability(.contentSize)
.defaultPosition(.topLeading)
.windowToolbarStyle(.unifiedCompact)
// MARK: 'Export Folder' single window
/// The 'Export Folder' window
Window("Export Folder with Songs…", id: AppSceneID.exportFolderWindow.rawValue) {
ExportFolderView()
.frame(width: 400, height: 600)
.environment(appState)
.environment(fileBrowser)
}
.keyboardShortcut("e")
/// Make it sizable by the View frame
.windowResizability(.contentSize)
.defaultPosition(.center)
.windowToolbarStyle(.unifiedCompact)
// MARK: 'Song' document window
/// The actual 'song' window
DocumentGroup(newDocument: ChordProDocument()) { file in
ContentView(document: file.$document, file: file.fileURL)
.environment(fileBrowser)
.environment(appState)
/// Give the scene access to the document.
.focusedSceneValue(\.document, file)
.onDisappear {
Task { @MainActor in
if let index = fileBrowser.openWindows.firstIndex(where: { $0.fileURL == file.fileURL }) {
/// Mark window as closed
fileBrowser.openWindows.remove(at: index)
}
}
}
.withHostingWindow { window in
/// Register the window unless we are browsing Versions
if
!(file.fileURL?.pathComponents.contains("com.apple.documentVersions") ?? false),
let window = window?.windowController?.window {
fileBrowser.openWindows.append(
NSWindow.WindowItem(
windowID: window.windowNumber,
fileURL: file.fileURL
)
)
}
}
.onChange(of: file.fileURL) { oldURL, newURL in
if let index = fileBrowser.openWindows.firstIndex(where: { $0.fileURL == oldURL }) {
fileBrowser.openWindows[index].fileURL = newURL
}
}
}
.commands {
CommandGroup(after: .appInfo) {
Divider()
Button("Export Folder with Songs…") {
openWindow(id: AppSceneID.exportFolderWindow.rawValue)
}
#if DEBUG
Divider()
ResetApplicationButtonView()
#endif
}
CommandGroup(after: .importExport) {
ExportSongView()
}
CommandGroup(after: .importExport) {
PrintPDFView()
}
CommandGroup(after: .textEditing) {
MenuButtonsView()
.environment(appState)
}
CommandGroup(replacing: .help) {
HelpButtonsView()
}
}
.defaultSize(width: 1000, height: 800)
.defaultPosition(.center)
// MARK: 'Song List' menu bar extra
/// Add Chord Provider to the Menu Bar
MenuBarExtra("Chord Provider", systemImage: "guitars") {
FileBrowserView()
.environment(fileBrowser)
.withHostingWindow { window in
fileBrowser.menuBarExtraWindow = window
}
}
.menuBarExtraStyle(.window)
// MARK: Settings
/// The settings window
Settings {
SettingsView()
.environment(appState)
.environment(fileBrowser)
.frame(width: 340, height: 480)
}
}
// MARK: Scene Windows
/// The window scenes we can open on macOS
///
/// - Note: The ID of a scene we set on on a `Window` or to open a `Window` in the environment is a `String`.
/// This is is asking for troubles, so I use an `enum` instead.
private enum AppSceneID: String {
/// A scene with a list of songs
case songListWindow
/// A scene to export a folder of songs
case exportFolderWindow
}
}
/// SwiftUI `View` with a `Button` to reset the application
public struct ResetApplicationButtonView: View {
/// Init the `View`
public init() {}
/// The body of the `View`
public var body: some View {
Button(
action: {
/// Remove user defaults
if let bundleID = Bundle.main.bundleIdentifier {
UserDefaults.standard.removePersistentDomain(forName: bundleID)
}
/// Delete the cache
let manager = FileManager.default
if let cacheFolderURL = manager.urls(
for: .cachesDirectory,
in: .userDomainMask
).first {
try? manager.removeItem(at: cacheFolderURL)
}
/// Terminate the application
NSApp.terminate(nil)
},
label: {
Text("Reset Application")
}
)
}
}